package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.vo.OrderSettingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author
 */
@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public List<OrderSettingVO> finAll(Integer year, Integer moth) {


        String orderDate = year + "-" + moth;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");

        Date date;
        try {
            date = dateFormat.parse(orderDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        orderDate = dateFormat.format(date);
        List<OrderSettingVO> orderSettingList = orderSettingDao.findAll(orderDate);


        return orderSettingList;
    }

    /**
     * 设置可预约人数
     *
     * @param orderSetting
     */
    @Override
    public void editNumByDate(OrderSetting orderSetting) {
        orderSettingDao.editNumberByOrderDate(orderSetting);

    }

    /**
     * 预约设置：上传文件
     *
     * @param orderSettingList
     */
    @Override
    public void insert(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {

            for (OrderSetting orderSetting : orderSettingList) {
                //检查此数据（日期）是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //已经存在，执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //不存在，执行添加操作
                    orderSettingDao.insert(orderSetting);
                }

            }
        }
    }

    @Override
    public boolean findByDate(Date orderDate) {
        OrderSetting orderSetting = orderSettingDao.findByDate(orderDate);
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if( reservations < number) {
            return false;//人未满
        }
      return  true;//人满了
    }
}


